package com.ohos.carbon.slice;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DataBean;
import com.ohos.carbon.slice.widgets.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.VectorElement;

import java.util.ArrayList;

import static com.ohos.carbon.slice.MainAbilitySlice.preferences;

public class WidgetsSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<DataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_widgets);
        initData();
        initView();
    }

    private void initData() {
        mList = new ArrayList<>();
        Element favour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_border_black_24dp);
        Element starFavour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_gray_24dp);
        Element circle = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_add_circle_black_24dp);
        VectorElement parse = (VectorElement) ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_wrap_text_black_24dp);
        Element checkbox = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_check_box_black_24dp);
        Element menu = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_menu_black_24dp);
        Element carbon = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_dropdown);
        Element view = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_view_stream_black_24dp);
        Element text_fields = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_text_fields_black_24dp);
        mList.add(new DataBean(null, "Widgets with sample styles, data and applications", null, null));
        mList.add(new DataBean(null, "Banner", null, preferences.getBoolean("Banner", false) ? starFavour : favour));
        mList.add(new DataBean(checkbox, "CheckBoxes & RadioButtons", null, preferences.getBoolean("CheckBoxes & RadioButtons", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Buttons", null, preferences.getBoolean("Buttons", false) ? starFavour : favour));
        mList.add(new DataBean(circle, "Floating Action Button", null, preferences.getBoolean("Floating Action Button", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Circular progress", null, preferences.getBoolean("Circular progress", false) ? starFavour : favour));
        mList.add(new DataBean(menu, "Menus", null, preferences.getBoolean("Menus", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Progress bars", null, preferences.getBoolean("Progress bars", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Snackbar", null, preferences.getBoolean("Snackbar", false) ? starFavour : favour));
        mList.add(new DataBean(text_fields, "Text fields", null, preferences.getBoolean("Text fields", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Tabs", null, preferences.getBoolean("Tabs", false) ? starFavour : favour));
        mList.add(new DataBean(view, "RecyclerView", null, preferences.getBoolean("RecyclerView", false) ? starFavour : favour));
        mList.add(new DataBean(null, "ExpandableRecyclerView", null, preferences.getBoolean("ExpandableRecyclerView", false) ? starFavour : favour));
        mList.add(new DataBean(null, "ExpansionPanel", null, preferences.getBoolean("ExpansionPanel", false) ? starFavour : favour));
        mList.add(new DataBean(carbon, "DropDown", null, preferences.getBoolean("DropDown", false) ? starFavour : favour));
        mList.add(new DataBean(null, "NavigationView", null, preferences.getBoolean("NavigationView", false) ? starFavour : favour));
        mList.add(new DataBean(null, "SeekBar", null, preferences.getBoolean("SeekBar", false) ? starFavour : favour));
        mList.add(new DataBean(parse, "FlowLayout & Chips", null, preferences.getBoolean("FlowLayout & Chips", false) ? starFavour : favour));
        mList.add(new DataBean(null, "TableLayout", null, preferences.getBoolean("TableLayout", false) ? starFavour : favour));
        mList.add(new DataBean(null, "BottomNavigationView", null, preferences.getBoolean("BottomNavigationView", false) ? starFavour : favour));
        mList.add(new DataBean(null, "BottomSheet", null, preferences.getBoolean("BottomSheet", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Backdrop", null, preferences.getBoolean("Backdrop", false) ? starFavour : favour));

    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Widgets");
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new WhatsProvider(mList, getContext()));
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                switch (i) {
                    case 1:
                        Intent intent = new Intent();
                        present(new BannerSlice(), intent);
                        break;
                    case 2:
                        Intent intent1 = new Intent();
                        present(new CheckBoxesSlice(), intent1);
                        break;
                    case 3:
                        Intent intent2 = new Intent();
                        present(new ButtonsSlice(), intent2);
                        break;
                    case 4:
                        Intent intent3 = new Intent();
                        present(new FloatingSlice(), intent3);
                        break;
                    case 5:
                        Intent intent4 = new Intent();
                        present(new CircularSlice(), intent4);
                        break;
                    case 6:
                        Intent intent5 = new Intent();
                        present(new MenusSlice(), intent5);
                        break;
                    case 7:
                        Intent intent6 = new Intent();
                        present(new ProgressSlice(), intent6);
                        break;
                    case 8:
                        Intent intent7 = new Intent();
                        present(new SnackbarSlice(), intent7);
                        break;
                    case 9:
                        Intent intent8 = new Intent();
                        present(new Text_filedsSlice(), intent8);
                        break;
                    case 10:
                        Intent intent9 = new Intent();
                        present(new TabsSlice(), intent9);
//                        Operation operation = new Intent.OperationBuilder()
//                                .withDeviceId("")
//                                .withBundleName("com.ohos.carbon.ability")
//                                .withAbilityName("com.ohos.carbon.ability.TabsAbility")
//                                .build();
//                        intent9.setOperation(operation);
//                        startAbility(intent9);
                        break;

                    case 11:
                        Intent intent10 = new Intent();
                        present(new RecyclerViewSlice(), intent10);
                        break;
                    case 12:
                        Intent intent11 = new Intent();
                        present(new ExpandableRecyclerViewSlice(), intent11);

                        break;
                    case 13:
                        Intent intent12 = new Intent();
                        present(new ExpansionPanelSlice(), intent12);
                        break;
                    case 14:
                        Intent intent13 = new Intent();
                        present(new DropDownSlice(), intent13);
                        break;
                    case 15:
                        Intent intent14 = new Intent();
                        present(new NavigationViewSlice(), intent14);
                        break;
                    case 16:
                        Intent intent15 = new Intent();
                        present(new SeekBarSlice(), intent15);
                        break;
                    case 17:
                        Intent intent16 = new Intent();
                        present(new FolwLayoutSlice(), intent16);
                        break;
                    case 18:
                        Intent intent17 = new Intent();
                        present(new TableLayoutSlice(), intent17);
                        break;
                    case 19:
                        Intent intent18 = new Intent();
                        present(new BottomNavigationSlice(), intent18);
                        break;
                    case 20:
                        Intent intent19 = new Intent();
                        present(new BottomSheetSlice(), intent19);
                        break;
                    case 21:
                        Intent intent20 = new Intent();
                        present(new BackdropSlice(), intent20);
                        break;
                }
            }
        });

        //返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
